package life.mahu.community.controller;

import com.github.pagehelper.PageInfo;
import life.mahu.community.dto.PageResultDTO;
import life.mahu.community.dto.ResultDTO;
import life.mahu.community.model.Question;
import life.mahu.community.service.QuestionService;
import life.mahu.community.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * question 后台数据控制层
 */
@Controller
@Slf4j
public class AdminQuestionController {

    @Autowired
    private QuestionService questionService;

    @Resource
    private RedisUtil redis;

    @GetMapping("/admin/question/list")
    public String getListHtml(){
        return "/admin/question/list";
    }
    @GetMapping("/admin/question/edit")
    public String getEditHtml(Model model,  @RequestParam("id") String id){
        model.addAttribute("questionId", id);
        return "/admin/question/edit";
    }
    @GetMapping("/admin/question/detail")
    public String getDetailHtml(Model model,  @RequestParam("id") String id){
        model.addAttribute("questionId", id);
        return "/admin/question/detail";
    }

    @GetMapping("/admin/question/add")
    public String getAddHtml(){
        return "/admin/question/edit";
    }

    @ResponseBody
    @GetMapping("/admin/question/delQuestion")
    public ResultDTO delUser(Model model,  @RequestParam("id") Long id){
        //从服务端查询数据
        Boolean success = questionService.delQuestionById(id);
        if(success){
            redis.del("mahuTag");
            return ResultDTO.okOf(200, "删除成功！");
        }else{
            return ResultDTO.okOf(201, "查无数据！");
        }
    }

    @ResponseBody
    @GetMapping("/admin/question/getList")
    public PageResultDTO getUserList(@RequestParam("keyword") String keyword,
                                     @RequestParam("categoryId") String categoryId,
                                     @RequestParam("uname") String uname,
                                     @RequestParam("tag") String tag,
                                     @RequestParam("limit") Integer limit,
                                     @RequestParam("page") Integer page){

        Question question = new Question();
        if(!("".equals(uname) || uname==null)){
            question.setUname(uname);
        }
        if(!("".equals(tag) || tag==null)){
            question.setTag(tag);
        }
        if(!("".equals(categoryId) || categoryId==null)){
            question.setCid(Integer.parseInt(categoryId));
        }
        //List<User> userList = userService.getUserList(user);
        PageInfo questionListPage = questionService.getQuestionListPage(question, page, limit, keyword);

        return PageResultDTO.okOf(0, "查询成功！", questionListPage.getTotal(), questionListPage.getList());
    }

    @ResponseBody
    @PostMapping("/admin/question/searchQuestionDetail")
    public ResultDTO getUserDetail(@RequestParam("id") Long id){

        Question question =  questionService.getQuestionById(id);

        return ResultDTO.okOf(question);

    }


    @ResponseBody
    @PostMapping("/admin/question/saveQuestion")
    public ResultDTO saveUser(HttpServletRequest request, HttpServletResponse response){

        ResultDTO resultDTO = new ResultDTO();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Question question = new Question();
            BeanUtils.populate(question, map);
            Boolean success = false;
            if(question.getId() == 0 || question.getId() == null){
                success = questionService.insertQuestion(question);
            }else{
                success = questionService.updateQuestionById(question);
            }
            if(success){
                redis.del("mahuTag");
                resultDTO.setCode(200);
                resultDTO.setMessage("保存成功！");
            }else{
                resultDTO.setCode(100);
                resultDTO.setMessage("系统繁忙！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultDTO.setCode(101);
            resultDTO.setMessage("服务器出错了！");
        }

        return resultDTO;

    }

}
